package com.ldatum.crm.util;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
public class BuildUtil {
	/**
	 * 根据表名生成实体类
	 * @param tableName
	 * @throws Exception
	 */
	public static void makeClass(String tableName) throws Exception{
		//获得列名
		List<String> columns = new ArrayList<String>();
		columns = ColumnUtil.GetColumnName(tableName);
		//获得列名的数据类型
		List<String> types = new ArrayList<String>();
		types = ColumnUtil.GetColumnType(tableName);
		//创建流
		String className = ConvertUtil.initialToUpper(tableName.substring(tableName.lastIndexOf("_")+1));//生成类名
		FileOutputStream fos = new FileOutputStream("src/com/ldatum/crm/domain/"+className+".java");
		OutputStreamWriter writer = new OutputStreamWriter(fos);
		PrintWriter pw = new PrintWriter(writer);
		pw.println("package com.ldatum.crm.domain;");
		pw.println("public class "+className+" {");
		int i = 0;
		String constructorPara = "";
		//生成属性列表
		while(true){
			String type = types.get(i);
			if(type.equalsIgnoreCase("varchar")||type.equalsIgnoreCase ("varchar2")||type.equalsIgnoreCase ("char")){
				type = "String";
			}
			if(type.equalsIgnoreCase("float")||type.equalsIgnoreCase("decimal")){
				type = "double";
			}
			if(type.equalsIgnoreCase("date")|| type.equalsIgnoreCase("datetime")){
				type = "Date";
			}
			String column = columns.get(i);
			i++;
			String propertyStr = "	private " + type +" "+ column + ";";
			//生成带参的构造函数所需要的内容
			if(!column.equals("id")){
				constructorPara += type + " " + column + ",";
			}
			pw.println(propertyStr);
			if(i == types.size()){
				break;
			}
		}
		//生成构造函数
		pw.println("	public "+className+"(){}");
		pw.println("	public "+className+"("+constructorPara.substring(0, constructorPara.length()-1)+") {");
		int x = 0;
		while(true){
			String column = columns.get(x++);
			if(!column.equals("id")){
				pw.println("		this."+column+" = " + column + ";");
			}
			if(x == columns.size()){
				break;
			}
		}
		pw.println("	}");
		//生成get和set方法
		int j = 0;
		int k = 0;
		while(true){
			String column = columns.get(j++);
			String type = types.get(k++);
			if(type.equals("varchar")){
				type = "String";
			}
			if(type.equalsIgnoreCase("varchar")||type.equalsIgnoreCase ("varchar2")||type.equalsIgnoreCase ("char")){
				type = "String";
			}
			if(type.equalsIgnoreCase("float")||type.equalsIgnoreCase("decimal")){
				type = "double";
			}
			if(type.equalsIgnoreCase("date")|| type.equalsIgnoreCase("datetime")){
				type = "Date";
			}
			pw.println("	public " + type + " get" + ConvertUtil.initialToUpper(column) +"() {");
			pw.println("		return " + column+";");
			pw.println("	}");
			pw.println("	public void set"+ConvertUtil.initialToUpper(column) +"("+type +" "+column+")" + " {");
			pw.println("		this."+column +" = "+column + ";");
			pw.println("	}");
			if(j == columns.size()){
				break;
			}
		}
		pw.println("}");
		pw.flush();
		pw.close();
		writer.close();
		fos.close();
		System.out.println("***生成实体类成功***");
	}
	/**
	 * 根据实体类的类名等信息，创建sql语句并执行
	 * @param className
	 * @throws Exception
	 */
	public static void makeTable(String obj) throws Exception{
		Class<?> c = Class.forName(obj);
		String clsName = c.getSimpleName();
		//获取所有属性
		Field[] fs = c.getDeclaredFields();
		StringBuffer sql = new StringBuffer();
		sql.append("create table t_"+clsName.toLowerCase()).append("("+"\n");
		int i = 0;
		while(true){
			String columnName = fs[i].getName();
			String columnType = fs[i].getType().getSimpleName();
			if(columnType.equals("String")){
				columnType = "varchar";
			}
			if(columnName.equals("id")){
				sql.append("	"+columnName+" ").append(columnType+" ").append("primary key auto_increment,"+"\n");
			}else{
				if(columnType.equals("varchar")){
					sql.append("	"+columnName+" ").append(columnType+"(255),"+"\n");
				}else{
					sql.append("	"+columnName+" ").append(columnType+",\n");
				}
			}
			i++;
			if(i==fs.length){
				break;
			}
		}
		String sql1 = sql.substring(0, sql.length()-2);
		sql1 = sql1+");";
		System.out.println("***生成sql语句成功***");
		/*
		 * 执行生成的sql语句
		 */
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DBUtil.open();
			pstmt = con.prepareStatement(sql1);
			boolean bool = pstmt.execute(sql1);//返回的是boolean值，如果执行的结果为ResultSet，则返回true,如果没有返回结果或者返回更新计数，则返回false
			if(bool){
				System.out.println("execute success!");
			}else{
				System.out.println("execute failed");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		makeClass("t_toDoItem");
	}
}
